package com.droidathletics.boundary.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class PerformanceCompetitionDAOAdapter {
	private static final String DATABASE_NAME = "droidathleticsdb.db";
	private static final int DATABASE_VERSION = 1;
	private static final String PERFORMANCECOMP_TABLE_NAME = "performancecomp";
	
    private static final String PERFORMANCECOMP_TABLE_CREATE =
        "CREATE TABLE " + PERFORMANCECOMP_TABLE_NAME + " (namesportsman TEXT, " +
        		"lastnamesportsman TEXT, namecompetition TEXT, timeone NUMERIC, " +
        		"timetwo NUMERIC, timethree NUMERIC, timefour NUMERIC, timefive NUMERIC, " +
        		"PRIMARY KEY(namesportsman, lastnamesportsman, namecompetition))";
	
    private SQLiteDatabase database;
	private final Context context;
    private PerformanceCompDBOpenHelper performanceCompHelper;
    
	public PerformanceCompetitionDAOAdapter(Context context) {
		this.context = context;
		performanceCompHelper = new PerformanceCompDBOpenHelper(this.context);
	    this.database = performanceCompHelper.getWritableDatabase();
	}
	
	public static class PerformanceCompDBOpenHelper extends SQLiteOpenHelper {
		
		public PerformanceCompDBOpenHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			db.execSQL(PERFORMANCECOMP_TABLE_CREATE);	
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + PERFORMANCECOMP_TABLE_NAME);
			onCreate(db);	
		}
	}

    public void close() {
    	performanceCompHelper.close();
    }
    
    public void deleteTable()
    {
    	this.database.delete(PERFORMANCECOMP_TABLE_NAME, null, null);
    }
    
    public long createPerformanceCompetition(String nameSportsman, String lastNameSportsman,
    		String nameCompetition) {
  
    	long result = -1;
    	ContentValues values = new ContentValues();
    	values.put("namesportsman", nameSportsman);
        values.put("lastnamesportsman", lastNameSportsman);
        values.put("namecompetition", nameCompetition);

        try {
            result = database.insertOrThrow(PERFORMANCECOMP_TABLE_NAME, null, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }

			
    public long updatePerformanceCompetition(String nameSportsman, String lastNameSportsman,
    		String nameCompetition) {
        
    	long result = -1;
    	ContentValues values = new ContentValues();
    	values.put("namesportsman", nameSportsman);
        values.put("lastnamesportsman", lastNameSportsman);
        values.put("namecompetition", nameCompetition);      
        
        try {
            result = database.update(PERFORMANCECOMP_TABLE_NAME, values, 
            		      "namesportsman='"+nameSportsman+"' AND " +
            		      "lastnamesportsman='"+lastNameSportsman+"' AND " +
            		      "namecompetition='"+nameCompetition+"'", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public long deletePerformanceCompetition(String nameSportsman, String lastNameSportsman,
    		String nameCompetition){
        long result = -1;
        
    	try {
            result = database.delete(PERFORMANCECOMP_TABLE_NAME, 
            	  "namesportsman='"+nameSportsman+"' AND " +
      		      "lastnamesportsman='"+lastNameSportsman+"' AND " +
      		      "namecompetition='"+nameCompetition+"'", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    	
    }

   
   public Cursor readPerformanceCompetition(String nameSportsman, String lastNameSportsman,
   		String nameCompetition){
	   return database.query(PERFORMANCECOMP_TABLE_NAME, new String[] {
				"namesportsman", "lastnamesportsman", "namecompetition",
				"timeone", "timetwo", "timethree", "timefour", "timefive"}, 
  		        "namesportsman='"+nameSportsman+"' AND " +
		        "lastnamesportsman='"+lastNameSportsman+"' AND " +
		        "namecompetition='"+nameCompetition+"'", null, null,
				null, null);
   
   }
}
